package me.jessyan.mvparms.demo.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaMetadata;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.aspsine.irecyclerview.RefreshHeaderLayout;
import com.aspsine.irecyclerview.widget.footer.LoadMoreFooterView;
import com.aspsine.irecyclerview.widget.header.ClassicRefreshHeaderView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.LoadingDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.data.entity.MessageListEntity;
import me.jessyan.mvparms.demo.di.component.DaggerMessageListComponent;
import me.jessyan.mvparms.demo.di.module.MessageListModule;
import me.jessyan.mvparms.demo.mvp.contract.MessageListContract;
import me.jessyan.mvparms.demo.mvp.presenter.MessageListPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.MessageListAdapter;
import me.jessyan.mvparms.demo.richeditor.RichEditorActivity;

public class MessageListActivity extends BaseActivity<MessageListPresenter> implements MessageListContract.View, OnLoadMoreListener, OnRefreshListener {

    @BindView(R.id.activity_message_list_recycler_view)
    IRecyclerView mMessageListRecyclerView;
    @Inject
    LinearLayoutManager mLinearLayoutManager;
    @Inject
    MessageListAdapter mMessageListAdapter;
    @Inject
    List<MessageListEntity> mMessageList;

    private String type = "1";
    private int page = 1;
    private int limit = 10;
    private LoadMoreFooterView loadMoreFooterView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMessageListComponent
                .builder()
                .appComponent(appComponent)
                .messageListModule(new MessageListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_message_list;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        page = 1;
        ArmsUtils.configRecyclerView(mMessageListRecyclerView, mLinearLayoutManager);
        mMessageListRecyclerView.setIAdapter(mMessageListAdapter);
        mPresenter.getMessageList(type, String.valueOf(page), String.valueOf(limit), true,true);
        mMessageListAdapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                Intent intent = new Intent(MessageListActivity.this, RichEditorActivity.class);
                intent.putExtra("messageId", mMessageList.get(position).getId());
                intent.putExtra("messageTitle", mMessageList.get(position).getTitle());
                launchActivity(intent);
            }
        });
        mMessageListRecyclerView.setOnRefreshListener(this);
        mMessageListRecyclerView.setOnLoadMoreListener(this);

        loadMoreFooterView = (LoadMoreFooterView) mMessageListRecyclerView.getLoadMoreFooterView();

    }

    @Override
    public void showLoading(String message) {
        LoadingDialog.showDialogForLoading(this, message, false);
    }

    @Override
    public void hideLoading() {
        LoadingDialog.cancelDialogForLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void startLoadMore() {
        if (loadMoreFooterView.canLoadMore() && mMessageListAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
        }
    }

    @Override
    public void endLoadMore() {
        if (loadMoreFooterView.canLoadMore() && mMessageListAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
        }
    }

    @Override
    public void noMore() {
        loadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
    }

    @Override
    public void startPullToRefresh() {
        mMessageListRecyclerView.setRefreshing(true);
    }

    @Override
    public void endPullToRefresh() {
        mMessageListRecyclerView.setRefreshing(false);
    }


    @Override
    public MessageListActivity getActivity() {
        return MessageListActivity.this;
    }

    @Override
    public int getLimit() {
        return limit;
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.getMessageList(type, String.valueOf(page), String.valueOf(limit), false,true);
        loadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.getMessageList(type, String.valueOf(page), String.valueOf(limit), false,false);
    }

}
